package com.zimi.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zimi.model.SysDuty;
import com.zimi.service.SysDutyService;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位职务表 api
 *
 * @author lnintr
 * @since 2019-05-22
 */
@RestController
@RequestMapping("sys-duty")
public class SysDutyApi {

    @Autowired
    private SysDutyService sysDutyService;

    @PostMapping("page")
    @ApiOperation("分页查询角色信息")
    public Object page(SysDuty duty, int page, int limit) {
        Page result = (Page) sysDutyService.pageList(duty, page, limit);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", result.getRecords());
        map.put("count", result.getTotal());
        return map;
    }

    @PostMapping("list")
    @ApiOperation("查询所有角色信息")
    public Object list() {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("List", sysDutyService.list());
        return map;
    }

    @PostMapping("add")
    @ApiOperation("添加角色")
    public Object add(SysDuty duty) {
        sysDutyService.insert(duty);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("update")
    @ApiOperation("修改角色")
    public Object upd(SysDuty duty) {
        sysDutyService.update(duty);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("delete")
    @ApiOperation("删除角色")
    public Object del(SysDuty duty) {
        sysDutyService.delete(duty);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("move")
    @ApiOperation("移动职务顺序")
    public Object up(HttpServletRequest request) {
        String[] ma = request.getParameterValues("dutyList");

        List<SysDuty> sysDutyList = GsonUtil.json2Objs(ma[0], SysDuty.class);
        Map map = new HashMap();
        map.put("code", sysDutyService.move(sysDutyList));
        return map;
    }

    @PostMapping("recycle")
    @ApiOperation("恢复角色")
    public Object del(HttpServletRequest request) {
        String[] ma = request.getParameterValues("dutyList");

        List<SysDuty> sysDutyList = GsonUtil.json2Objs(ma[0], SysDuty.class);
        Map map = new HashMap();
        map.put("code", sysDutyService.recycle(sysDutyList));
        return map;
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public Object getDutyById(@PathVariable("id") int id) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("sysDuty", sysDutyService.selectById(id));
        return map;
    }
}
